/**
 *
 */
package com.storm.lib.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 类功能: 字符串工具类
 * @author johnny
 * @version 2010-10-28
 */
public class StringUtil {

	
	/**
	 * 方法功能:字符串拼接
	 * 更新时间:2010-10-28, 作者:johnny
	 * @param fieldStrs
	 * @param string
	 * @return
	 */
	public static String implode(String delim,List<Integer> ary) {
		if(ary.size()<=0){
			return null;
		}
	    StringBuffer out = new StringBuffer();
	    for(int i=0; i<ary.size(); i++) {
	        if(i!=0) { out.append(delim); }
	        out.append(ary.get(i));
	    }
	    return out.toString();
	}
	/**
	 * 方法功能:字符串拼接
	 * 更新时间:2010-10-28, 作者:johnny
	 * @param fieldStrs
	 * @param string
	 * @return
	 */
	public static String implode(String[] ary, String delim) {
		if(ary.length<=0){
			return null;
		}
	    StringBuffer out = new StringBuffer();
	    for(int i=0; i<ary.length; i++) {
	        if(i!=0) { out.append(delim); }
	        out.append(ary[i]);
	    }
	    return out.toString();
	}

	/**
	 * 方法功能:字符串拼接
	 * 更新时间:2010-10-28, 作者:johnny
	 * @param fieldStrs
	 * @param string
	 * @return
	 */
	public static String implode(int[] ary, String delim) {
		if(ary.length<=0){
			return null;
		}
		StringBuffer out = new StringBuffer();
	    for(int i=0; i<ary.length; i++) {
	        if(i!=0) { out.append(delim); }
	        out.append(ary[i]);
	    }
	    return out.toString();
	}
	/**
	 * 功能:字符串拼接
	 * @param ary
	 * @param delim
	 * @return
	 */
	public static String implode(Integer[] ary, String delim) {
		if(ary.length<=0){
			return null;
		}
		StringBuffer out = new StringBuffer();
	    for(int i=0; i<ary.length; i++) {
	        if(null!=ary[i] || ary[i]>0){
	        	if(i!=0) {
	        		out.append(delim);
	        	}
	        	out.append(ary[i]);
	        }
	    }
	    return out.toString();
	}

	/**
	 * 方法功能:字符串拼接
	 * 更新时间:2010-10-28, 作者:johnny
	 * @param fieldStrs
	 * @param string
	 * @return
	 */
	public static String implode(List<String> ary, String delim) {
		if(ary.size()<=0){
			return null;
		}
		StringBuffer out = new StringBuffer();
	    for(int i=0; i<ary.size(); i++) {
	        if(i!=0) { out.append(delim); }
	        out.append(ary.get(i));
	    }
	    return out.toString();
	}

	/**
	 * 方法功能:a_b 转换为 aB
	 * 更新时间:2010-10-28, 作者:johnny
	 * @param fieldName
	 * @return
	 */
	public static String getJavaStyleString(String fieldName) {
		int index = 0;
		char[] chars = fieldName.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			char current = chars[i];
			if(current=='_'){
				if((i+1)<=chars.length-1)
				{
					String value = String.valueOf(chars[i+1]);
					value=value.toUpperCase();
					chars[i+1]=value.charAt(0);
				}
			}
		}
		String result =String.valueOf(chars);
		result=result.replaceAll("_", "");
		return result;
	}

	/**
	 * 方法功能:
	 * 更新时间:2010-10-28, 作者:johnny
	 * @param javaFieldName
	 * @return 首字母大写
	 */
	public static String firstUpper(String javaFieldName) {
		char[] chars =javaFieldName.toCharArray();
		String single =String.valueOf(chars[0]).toUpperCase();
		chars[0] = single.charAt(0);
		return String.valueOf(chars);
	}

	/**
	 * 方法功能:
	 * 更新时间:2010-10-28, 作者:johnny
	 * @param javaFieldName
	 * @return 首字母大写
	 */
	public static String firstDowner(String javaFieldName) {
		char[] chars =javaFieldName.toCharArray();
		String single =String.valueOf(chars[0]).toLowerCase();
		chars[0] = single.charAt(0);
		return String.valueOf(chars);
	}

	/**
	 *
	 * 更新时间:2010-11-21, 作者:johnny
	 * 方法功能:如果为null或者""(可带空格)都返回true
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		if(str==null){
			return true;
		}
		if("".equals(str.trim())){
			return true;
		}
		return false;
	}

	/**
	 *
	 * 更新时间:2010-11-21, 作者:johnny
	 * 方法功能:如果为null或者""(可带空格)都返回true
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	/**
	 *
	 * 更新时间:2010-11-21, 作者:johnny
	 * 方法功能:返回类似1,2,3,4等逗号分隔的数值
	 * @param str
	 * @return 如果是null,"",返回null
	 */
	public static int[] getCommerIntsString(String str) {
		if(isEmpty(str)){
			return null;
		}
		else{
			String[] values = str.split(",");
			int[] returns = new int[values.length];
			for (int i = 0; i < returns.length; i++) {
				returns[i] = Integer.valueOf(values[i]);
			}
			return returns;
		}
	}

	/**
	 * 将一个字符串转换成一个Map
	 * @param str 格式:1|1000_1001_1002,2|1000_1001_1002,
	 * @return
	 */
	public static ConcurrentHashMap<Integer, List<Integer>> getMapByStr(String str){
		ConcurrentHashMap<Integer, List<Integer>> map = new ConcurrentHashMap<Integer, List<Integer>>();
		if(StringUtil.isEmpty(str))
			return map;
		String[] strs = str.split("[,]");
		for(int i=0; i<strs.length; i++){
			String[] fields = strs[i].split("|");
			Integer field = Integer.valueOf(fields[0]);
			String[] ids = fields[1].split("[,]");
			List<Integer> roleIdList = new ArrayList<Integer>();
			for(int j=0; i<ids.length; j++){
				roleIdList.add(Integer.valueOf(ids[j]));
			}
			map.put(field, roleIdList);
		}
		return map;
	}

	/**
	 * 将一个Map转换成一个字符串
	 * @param map
	 * @return
	 */
	public static String getStrByMap(Map<Integer, List<Integer>> map){
		if(map==null){
			return "";
		}
		Iterator iter = map.entrySet().iterator();
		String str = "";
		while(iter.hasNext()){
			Map.Entry entry = (Map.Entry) iter.next();
			Integer i = (Integer) entry.getKey();
			//List<Integer> list = map.get(i);
			List<Integer> list = (List<Integer>)entry.getValue();
			str += i.toString() + "|";
			String out = "";
			for(int j=0; j<list.size(); j++){
		        if(j!=0) { out += "_"; }
		        out += list.get(j);
			}
			str += out + ",";
		}
		return str;
	}
	/**
	 * 将一个Map转换成一个字符串
	 * @param map
	 * @return
	 */
	public static String implode(Map<Integer, List<String>> map){
		if(map==null){
			return "";
		}
		Iterator iter = map.entrySet().iterator();
		String str = "";
		while(iter.hasNext()){
			Map.Entry entry = (Map.Entry) iter.next();
			Integer key = (Integer)entry.getKey();
			List<String> list = (List<String>) entry.getValue();
			String out = key + "|" + implode(list, ",");
			str += out + ";";
		}
		return str;
	}

	/**
	 * 将一个字符串转换成一个List
	 * @param Str
	 * @return
	 */
	public static List<Integer> getListByStr(String str) {
		List<Integer> list = new ArrayList<Integer>();
		if(isEmpty(str)){
			return list;
		}
		String[] strs = str.split("[,]");
		for(int i=0; i<strs.length; i++){
			list.add(Integer.valueOf(strs[i]));
		}
		return list;
	}

	/**
	 * 方法功能:将一个list转换成字符串
	 * @author future 2011-10-17
	 * @param list
	 * @param delim
	 * @return
	 */
	public static String getStrByList(List<Integer> list,String delim) {
		if(null==list)return "";
		String out = "";
	    for(int i=0; i<list.size(); i++) {
	        if(i!=0) { out += delim; }
	        out += list.get(i);
	    }
	    return out;
	}

	public static boolean notEqual(String s1, String s2) {
		if(equal(s1,s2)==true){
			return false;
		}
		return true;
	}


	public static boolean equal(String s1, String s2) {
		if(s1==null || s2==null){
			if(s1==null && s2==null){
				return true;
			}
			else{
				return false;
			}
		}
		if(s1.equals(s2)){
			return true;
		}
		return false;
	}
	/**
	 * 过滤数组中的空
	 * @param strs
	 */
	public static String[] filterNull(String[] strs) {
		if(null==strs || strs.length<=0){
			return null;
		}
		List<String> list = new ArrayList<String>();
		for(int i=0; i<strs.length; i++){
			String s = strs[i];
			if(null!=s && s.trim().length()>0){
				list.add(s);
			}
		}
		if(list.size()>0){
			strs = new String[list.size()];
			for(int i=0; i<list.size(); i++){
				strs[i] = list.get(i);
			}
			return strs;
		}
		return null;
	}
	/**
	 * 替换字符串
	 * @param totalStr
	 * @param oldStr
	 * @param newStr
	 * @return
	 */
	public static String replaceStr(String totalStr, String oldStr) {
		String str = "";
		String newStr = oldStr.substring(0, oldStr.indexOf(":")+1);
		if(totalStr.trim().length()<=0){
			str = newStr;
		}else{
			char c = totalStr.charAt(totalStr.length()-1);
			if(c!=';'){
				str = totalStr+";"+newStr;
			}else{
				str = totalStr+newStr;
			}
		}
		return str;
	}
	/**
	 * 判断字符串中是否包含某字符串
	 * @param idStr
	 * @param strs 格式(1234,4567,5678);
	 * @return
	 */
	public static boolean checkInStr(Integer id, String strs) {
		if(strs==null || strs.length()<=0){
			return false;
		}
		int[] ints = getCommerIntsString(strs);
		for(int i=0; i<ints.length; i++){
			if(id.intValue()==ints[i]){
				return true;
			}
		}
		return false;
	}

	public static Map<Integer, List<String>> getMap(String str) {
		if(null==str || str.length()<=0){
			return null;
		}
		Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
		String[] items = str.split("\\;");
		for(int i=0; i<items.length; i++){
			String item = items[i];
			String[] infos = item.split("\\|");
			Integer roleId = Integer.valueOf(infos[0]);
			String[] heroIds = infos[1].split("\\,");
			List<String> list = Arrays.asList(heroIds);
			map.put(roleId, list);
		}
		return map;
	}
	/**
	 * 过滤字符串
	 * @param word
	 * @return
	 */
	public static boolean filterWord(String word){
		String special_regex = "[^\\s_@\\\\/]*";//特殊字符:空格  _ @ \ /
		String fuck_regex = "((?!fuck).)*"; //含有fuck的字符
		String shit_regex = "((?!shit).)*";//含有shit的字符
		Pattern pattern = Pattern.compile(special_regex,Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(word);
		if(!matcher.matches()){
			return false;
		}
		pattern = Pattern.compile(fuck_regex,Pattern.CASE_INSENSITIVE);
		matcher = pattern.matcher(word);
		if(!matcher.matches()){
			return false;
		}
		pattern = Pattern.compile(shit_regex,Pattern.CASE_INSENSITIVE);
		matcher = pattern.matcher(word);
		if(!matcher.matches()){
			return false;
		}
		return true;
	}

	public static void main(String[] args) {
		String[] strs = new String[3];
		strs[2] = "aaaa";
		System.out.println(implode(strs, ","));
//		String word = "Fduck";
//		if(filterWord(word)){
//			System.out.println("ok");
//		}else{
//			System.out.println("no");
//		}
	}
}
